package com.linewell.demo;

import net.sf.json.JSONObject;
import org.junit.Test;
import org.springframework.data.mongodb.core.query.Update;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Title: IdeaProject
 * @description:
 * @author: hyuhuang@linewell.com
 * @since:2018/8/1
 */
public class Study {
    static int count=0;
    static ExecutorService executor= Executors.newFixedThreadPool(100);      //创建线程池大小为100个线程的线程池
    static CountDownLatch latch=new CountDownLatch(100);    //计数器

    @Test
    public void collections() {
        List list = new ArrayList();    //不带初始化ArrayList  默认为16个长度。若不够则翻倍初始化控件，直到够了，
        List list2 = new ArrayList(6);
        //  初始化长度为6的内存空间。
        list.retainAll(list2);   // 和list2中相同的集合。
        list.removeAll(list2);   //移除 list2里的集合。
        list.add("1");
        list.add("2");
        list.subList(0, 2);  //集合截取，subList和List  如果subList没有被垃圾回收，
        // List是不会被修改，subList如果被修改了。那被截取的List也会相同变化。
        Arrays.asList();  //返回的类名虽然也是ArrayList，但包名不同。这个List是只读的
		/*Queue blockingQueue = new BlockingQueue();
		list.sort();
		Collections.binarySearch()           //先排序 才能使用二分查找。
		Arrays.binarySearch();    */
        Integer e = 31;
        Integer f = 31;//
        System.out.println(e == f);
        Objects.equals("", "");    //这个工具类已经对比较的俩个数进行了判空操作。
        String s = new String();
        s.intern();                    //放入常量池，共用同一个内存空间。
        s.join(",", "key:value", "key:value");    //拼接json。
        s.split(",", 5);     //限制分割几个，分割到5个后  接下来就不分割了。
        list.toArray();
        new ArrayList<>(Arrays.asList(list));
        //new Thread  占用1m内存
    }

    @Test
    public void testThread() throws InterruptedException {
            for(int i=0;i<100;i++) {
//                synchronized (executor) {    //加锁
                    executor.execute(() -> {
                        for (int j = 0; j < 10000; j++) {
                            count++;
                        }
                        latch.countDown();
                    });
//                }
            }
            latch.await();
            executor.shutdownNow();
            System.out.println(count);
    }


    //文件流之将hello world写入到文件
    @Test
    public void InStream() throws Exception{

        File f = new File("D://123.txt");
        //读取文件的byte
        byte[] bs = Files.readAllBytes(f.toPath());
        String str = "hello world";
        //将读取byte写入到 D://123.txt;
        Files.write(f.toPath(),str.getBytes());

        //建一个1000的
        ByteBuffer bf = ByteBuffer.allocate(1000);
        //将一个put
        bf.put((byte)'a');
        bf.put((byte)97);
        bf.flip();
        System.out.println(bf.get());

        Paths.get("c","txt.txt");

       /* try {
            FileInputStream fileInputStream = new FileInputStream(new File("D://123.txt"));
            BufferedInputStream inputStream = new BufferedInputStream(fileInputStream);

            String str = "hellow world";
            byte[] bytes = str.getBytes();
            try {
                inputStream.read(bytes);
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }*/
    }

    @Test
    public void test(){
//        String[] strings = new String[]{"1","2","3"};
//        System.out.println(String.join(",", strings));
        Update update = new Update();
        update.set("124124","111111");
        update.set("124124","124124");
        System.out.println(update.toString());
    }
}
